package algorithm_demo.leetcode;

/**
 * 1047. 删除字符串中的所有相邻重复项
 * <a href="https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/">https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/</a>
 *
 * @author Api
 * @date 2023/5/9 22:45
 */
public class Problem_1047_RemoveAllAdjacentDuplicatesInString {
    public String removeDuplicates(String s) {
        if (s == null || s.length() == 0) {
            return "";
        }
        char[] arr = s.toCharArray();
        int i = -1;
        int j = 0;
        while (j < arr.length) {
            if (i>0 && arr[i] == arr[j]){
                i--;
            }else{
                i++;
                arr[i] = arr[j];
            }
            j++;
        }
        return String.copyValueOf(arr, 0, i+1);
    }
}
